<?php

namespace app\home\controller;

use think\Controller;
use think\Loader;
use think\Request;
use think\Db;
use think\Session;
use PHPMailer\PHPMailer\PHPMailer;


class Login extends Controller
{
    public function index()
    {
        if (!Session::get('user')) {
            $ses = [];
            $ses ['vip'] = 3;
        }else{
            $this->success('无需重复登录','/home/index/index');
        }
        $fenlei = Db::table('mh_fenlei')->select();
        $list = db('in_user')->field(['id','name'])->order('id','ASC')->select();
        $link = Db::table('mh_ad_link')->select();
        return view('login/denglu', [
            'title' => '登陆页','list'=>$list,'ses'=>$ses,'link'=>$link,'fenlei'=>$fenlei
        ]);
    }

    public function regist()
    {
        $fenlei = Db::table('mh_fenlei')->select();
        if (!Session::get('user')) {
            $ses = [];
            $ses ['vip'] = 3;
        }else{
            $this->success('无需重复登录','/home/index/index');
        }
        return view('regist/regist',['title'=>'注册页','ses'=>$ses,'fenlei'=>$fenlei]);
    }

    public function main()
    {
        $list = db('ad_user')->field(['id','name','status'])->order('id','ASC')->select();
        if (!Session::get('user')) {
            $ses = [];
            $ses ['vip'] = 3;
        }else{
            $this->success('无需重复登录','/home/index/index');
        }
        return view('main/index', [
            'title' => '登陆页','list'=>$list,'time'=>date('Y/m/d',time()),'ses'=>$ses
        ]);
    }

    public function email()
    {
        $fenlei = Db::table('mh_fenlei')->select();
        $link = Db::table('mh_ad_link')->select();
        if (!Session::get('user')) {
            $ses = [];
            $ses ['vip'] = 3;
        }else{
            $this->success('无需重复登录','/home/index/index');
        }
            return view('login/email1',['title'=>'邮箱注册','ses'=>$ses,'link'=>$link,'fenlei'=>$fenlei]);
    }


    public function doemail()
    {
        $data['email'] = $_GET['email'];
        $data1 = $data['email'];
        $token = md5($data['email']);
        // $token1 = ($data['email']);
        Session::set('bbc',$data);
        // var_dump(Session::get('bbc'));die;
        $url = 'http://47.100.33.39/confirm.html?token'.$token;
        
       
        $mail = new PHPMailer();
try {
//Server settings
$mail->SMTPDebug = 0;
$mail->isSMTP();
$mail->Host = 'smtp.163.com';
$mail->Charset = 'UTF-8';
$mail->SMTPAuth = true;
$mail->Username = '13186525453@163.com';
$mail->Password = '88595832dtl';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
//Recipients
$mail->setFrom('13186525453@163.com', 'Mailer');
$mail->addAddress($data1, 'qq');
// $mail->addAddress('ellen@example.com');
// $mail->addReplyTo('info@example.com', 'Information
// $mail->addCC('cc@example.com');
// $mail->addBCC('bcc@example.com');
// //Attachments
// $mail->addAttachment('/var/tmp/file.tar.gz');
// $mail->addAttachment('/tmp/image.jpg', 'new.jpg');
//Content
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
// $mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->Body = 'This is 测试 <a href = '.$url.'>邮件</a>'; 
$mail->send();
echo '请去邮箱验证';


} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ';
}


    }



    public function yanzheng()
    {
        return view('regist/register',['title'=>'详细信息']);
    }

    public function doregist()
    {
        // Request::instance()->session('bbc');
        // Session::start('bbc');
        $emaill=Session::get('bbc');
        
        // 调用validate 里的User类
        $validate = Loader::validate('User');
        // var_dump($validate);
        $name = $_POST['name'];
        $email = $emaill['email'];
        $tel = $_POST['tel'];
        $sex = $_POST['sex'];
        $pass = md5($_POST['pwd']);
        $repass = md5($_POST['pwd_confirm']);
        $time = date('Y/m/d',time());
        $pass1 = $_POST['pwd'];
        $repass1 = $_POST['pwd_confirm'];

       

        $data = [

                'name' => $name,
                'email' => $email,
                'tel' => $tel,
                'sex' => $sex,
                'pass' => $pass1,
                'pass_confirm' => $repass1,
                'status' => '0'

        ];


        $data1 = [

                'name' => $name,
                'email' => $email,
                'tel' => $tel,
                'sex' => $sex,
                'pass' => $pass,
                'status' => '0'

        ];




        // $data1 = [
        //         'name' => $name,
        //         'tel' => $tel,
        //         'sex' => $sex,
        //         'pwd' => $pass,
        //         'createtime' => $time,
        //         'status' => '1'

        // ];

        // 正则验证匹配
        if(!$validate->check($data)){
            dump($validate->getError());
            // 不匹配返回错误信息
        }else{
            $result = db('in_user')->insert($data1);
            if($result){
            $this->success('注册成功','home/login/index');
        }
       }

   }

   public function do_login(){
        $data = $_POST;
        $data['pass'] = md5($data['pass']);
        $res = DB::table('mh_in_user')->where(['email'=>$data['email'],'pass'=>$data['pass']])->find();
        if($res){
            Session::set('user',$res);
            $this->success('登录成功', 'home/index/index');
            } else {
            $this->error('登录失败');
            }

   }


   public function tuichu(){
       session::delete('user');

    if (!Session::get('user')) {
        $ses = [];
        $ses ['vip'] = 3;
    }
    $list = db('in_user')->field(['id','name'])->order('id','ASC')->select();
    $fenlei = Db::table('mh_fenlei')->select();
    $link = Db::table('mh_ad_link')->select();
    return view('login/denglu', [
        'title' => '登陆页','list'=>$list,'ses'=>$ses,'fenlei'=>$fenlei,'link'=>$link
    ]);
   }

 


    


    
    }